import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:ball_lion/module/expert/items/league_hot_expert.dart';
import 'package:ball_lion/module/match/detail/lq_match_detail_logic.dart';
import 'package:ball_lion/module/match/detail/zq_match_detail_logic.dart';
import 'package:ball_lion/entity/expert/zq_hot_expert_entity.dart';
import 'package:ball_lion/entity/other/pagging_data.dart';
import 'package:ball_lion/entity/match/match_live_entity.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/entity/match/expert_views_entity.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/utils/sp_utils.dart';
import 'package:ball_lion/utils/utils.dart';

class MatchViewsLogic extends GetxController with GetTickerProviderStateMixin {
  String _matchId = "";
  ZqMatchDetailLogic? zqDetail;
  LqMatchDetailLogic? lqDetail;
  late int sportsId;
  List<String> tabs = ["关注", "人气", "免费", "回报", "连红"];
  List<int> tabIds = [10, 9, 5, 3, 4];
  int _tabIndex = 4;
  bool _checkLogin = true;
  bool _visible = false;
  bool _isLoading = true;
  List<PagingData<List<PlanEntity>?>> _pages =
      List.generate(5, (index) => PagingData<List<PlanEntity>?>(null, 1, 15));
  List<PlanEntity>? livePlan;
  final EasyRefreshController _refreshController =
      EasyRefreshController(controlFinishLoad: true);
  List<ResourceEntity> carousel = [];
  TabController? tabController;
  List<HotExpertEntity>? expertTop;
  MatchLiveEntity? _liveInfo;
  HotBoardController? boardController;

  bool get visible => _visible;
  bool get checkLogin => _checkLogin;
  bool get isLoading => _isLoading;
  int get leagueId =>
      (sportsId == 1
          ? zqDetail?.info?.baseInfo?.leagueId
          : lqDetail?.detail?.leagueId?.toInt()) ??
      0;
  String get leagueName =>
      (sportsId == 1
          ? zqDetail?.info?.baseInfo?.nameChsShort
          : lqDetail?.detail?.leagueName) ??
      "";
  MatchLiveEntity? get liveInfo =>
      _liveInfo ?? (sportsId == 1 ? zqDetail?.liveInfo : lqDetail?.liveInfo);
  EasyRefreshController get refreshController => _refreshController;
  List<PagingData<List<PlanEntity>?>> get pages => _pages;
  int get tabIndex => _tabIndex;
  String get matchId => _matchId;

  set matchId(String value) {
    _matchId = value;
    update();
  }

  set liveInfo(MatchLiveEntity? value) {
    _liveInfo = value;
    update();
  }

  set tabIndex(value) {
    _tabIndex = value;
    if (1 == sportsId) {
      SpUtils.matchDetailZqTabIndex = value;
    } else {
      SpUtils.matchDetailLqTabIndex = value;
    }

    tabController?.animateTo(_tabIndex);
    update();
  }

  @override
  void onReady() async {
    super.onReady();

    await Future.delayed(const Duration(milliseconds: 200));
    Utils.onEvent('bs_fa');
    try {
      zqDetail = Get.find<ZqMatchDetailLogic>(tag: matchId);
      sportsId = 1;
      _tabIndex = SpUtils.matchDetailZqTabIndex ?? 4;
    } catch (e) {
      lqDetail = Get.find<LqMatchDetailLogic>(tag: matchId);
      sportsId = 2;
      _tabIndex = SpUtils.matchDetailLqTabIndex ?? 2;
    }
    tabController = TabController(
        length: _pages.length, vsync: this, initialIndex: _tabIndex);
    boardController = HotBoardController.league(
        sportsId: sportsId, leagueId: leagueId, leagueName: leagueName);
    update();
    getData(tabIndex);
  }

  Future getData(int index) async {
    if (carousel.isEmpty) {
      var list = await OtherApi.getAppList("app_ads");
      list?.forEach((element) {
        if (element.button == "match_views_ads") {
          carousel.add(element);
        }
      });
    }
    if (_pages[index].page != 1) return;
    List<PlanEntity> data = await ExpertApi.getMatchHotPlan(
        _pages[_tabIndex].page,
        _pages[_tabIndex].pageSize,
        tabIds[_tabIndex],
        sportsId,
        matchId: matchId.toInt());
    _pages[index].items = data;

    expertTop = await ExpertApi.getMatchExpertHot(leagueId, sportsId);
    _isLoading = false;
    update();
  }

  Future getRefresh() async {
    carousel = [];
    var list = await OtherApi.getAppList("app_ads");
    list?.forEach((element) {
      if (element.button == "match_views_ads") {
        carousel.add(element);
      }
    });
    boardController?.getData();
    _pages[_tabIndex].page = 1;
    List<PlanEntity> data = await ExpertApi.getMatchHotPlan(
        _pages[_tabIndex].page,
        _pages[_tabIndex].pageSize,
        tabIds[_tabIndex],
        sportsId,
        matchId: matchId.toInt());
    _pages[_tabIndex].items = data;
    update();
  }

  Future loadMore() async {
    _pages[_tabIndex].page += 1;
    List<PlanEntity> data = await ExpertApi.getMatchHotPlan(
        _pages[_tabIndex].page,
        _pages[_tabIndex].pageSize,
        tabIds[_tabIndex],
        sportsId,
        matchId: matchId.toInt());
    _pages[_tabIndex].items?.addAll(data);

    refreshController.finishLoad(
        (_pages[_tabIndex].items?.length ?? 0) < _pages[_tabIndex].total
            ? IndicatorResult.noMore
            : IndicatorResult.success);
    update();
  }
}
